import sys
import os
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QPushButton, QVBoxLayout,
                             QHBoxLayout, QLabel, QLineEdit, QMessageBox, QStackedWidget,
                             QGroupBox, QFormLayout, QDialog, QTabWidget, QTableWidget,
                             QTableWidgetItem, QHeaderView, QComboBox, QTextEdit, QGridLayout)
from PyQt5.QtCore import Qt, pyqtSignal, QSize
from PyQt5.QtGui import QFont, QIcon

# 添加项目根目录到系统路径
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from database import Database

# 学生窗口
class StudentWindow(QMainWindow):
    def __init__(self, user):
        super().__init__()
        self.user = user
        self.initUI()
        
    def initUI(self):
        self.setWindowTitle('宿舍管理系统 - 学生界面')
        self.setGeometry(100, 100, 800, 600)
        
        # 创建中央窗口部件
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        
        # 创建主布局
        main_layout = QVBoxLayout(central_widget)
        
        # 欢迎信息
        # 根据user对象的类型处理显示信息
        if isinstance(self.user, dict):
            name = self.user.get('name') or self.user.get('username')
            role = self.user.get('role')
        elif isinstance(self.user, tuple) and len(self.user) >= 3:
            name = self.user[3] if len(self.user) > 3 and self.user[3] else self.user[1]
            role = self.user[2]
        else:
            name = "用户"
            role = "未知"
            
        welcome_label = QLabel(f'欢迎, {name}! 角色: {role}')
        welcome_label.setAlignment(Qt.AlignCenter)
        welcome_font = QFont()
        welcome_font.setPointSize(12)
        welcome_label.setFont(welcome_font)
        main_layout.addWidget(welcome_label)
        
        # 功能按钮布局
        button_layout = QGridLayout()
        
        # 创建功能按钮 - 学生只能访问特定功能
        self.dormitory_btn = QPushButton('查询宿舍信息')
        self.dormitory_btn.clicked.connect(self.open_dormitory_query)
        button_layout.addWidget(self.dormitory_btn, 0, 0)
        
        self.allocation_btn = QPushButton('查询宿舍分配情况')
        self.allocation_btn.clicked.connect(self.open_allocation_query)
        button_layout.addWidget(self.allocation_btn, 0, 1)
        
        self.report_btn = QPushButton('报修宿舍')
        self.report_btn.clicked.connect(self.open_maintenance_report)
        button_layout.addWidget(self.report_btn, 1, 0)
        
        self.maintenance_btn = QPushButton('维修记录查询')
        self.maintenance_btn.clicked.connect(self.open_maintenance_query)
        button_layout.addWidget(self.maintenance_btn, 1, 1)
        
        self.fee_btn = QPushButton('查询宿舍费用')
        self.fee_btn.clicked.connect(self.open_fee_query)
        button_layout.addWidget(self.fee_btn, 2, 0)
        
        # 设置按钮大小
        for i in range(button_layout.count()):
            button_layout.itemAt(i).widget().setMinimumHeight(80)
            button_layout.itemAt(i).widget().setFont(QFont('Arial', 12))
        
        # 添加按钮布局到主布局
        button_group = QGroupBox('功能菜单')
        button_group.setLayout(button_layout)
        main_layout.addWidget(button_group)
        
        # 退出按钮
        self.exit_btn = QPushButton('退出系统')
        self.exit_btn.clicked.connect(self.close)
        main_layout.addWidget(self.exit_btn)
    
    def open_dormitory_query(self):
        """打开宿舍查询窗口"""
        from iteration1_dormitory_management.gui_dormitory_management import DormitoryManagementWindow, QueryDormitoryWidget
        # 直接创建查询窗口组件
        self.query_widget = QueryDormitoryWidget()
        # 创建一个简单的窗口来容纳查询组件
        self.dormitory_window = QMainWindow()
        self.dormitory_window.setWindowTitle('宿舍信息查询')
        self.dormitory_window.setGeometry(100, 100, 800, 600)
        self.dormitory_window.setCentralWidget(self.query_widget)
        # 添加返回按钮
        back_btn = QPushButton('返回')
        back_btn.clicked.connect(self.dormitory_window.close)
        layout = QVBoxLayout()
        layout.addWidget(self.query_widget)
        layout.addWidget(back_btn)
        central_widget = QWidget()
        central_widget.setLayout(layout)
        self.dormitory_window.setCentralWidget(central_widget)
        self.dormitory_window.show()
    
    def open_allocation_query(self):
        """打开宿舍分配查询窗口"""
        from iteration3_dormitory_allocation.gui_dormitory_allocation import DormitoryAllocationWindow, QueryAllocationWidget
        # 直接创建查询窗口组件
        self.allocation_query_widget = QueryAllocationWidget()
        # 创建一个简单的窗口来容纳查询组件
        self.allocation_window = QMainWindow()
        self.allocation_window.setWindowTitle('宿舍分配查询')
        self.allocation_window.setGeometry(100, 100, 800, 600)
        # 添加返回按钮
        back_btn = QPushButton('返回')
        back_btn.clicked.connect(self.allocation_window.close)
        layout = QVBoxLayout()
        layout.addWidget(self.allocation_query_widget)
        layout.addWidget(back_btn)
        central_widget = QWidget()
        central_widget.setLayout(layout)
        self.allocation_window.setCentralWidget(central_widget)
        self.allocation_window.show()
    
    def open_maintenance_report(self):
        """打开宿舍报修窗口"""
        from iteration4_maintenance_management.gui_maintenance_management import ReportMaintenanceDialog
        self.report_dialog = ReportMaintenanceDialog()
        self.report_dialog.exec_()
    
    def open_maintenance_query(self):
        """打开维修记录查询窗口"""
        from iteration4_maintenance_management.gui_maintenance_management import MaintenanceManagementWindow, QueryMaintenanceWidget
        # 直接创建查询窗口组件
        self.maintenance_query_widget = QueryMaintenanceWidget()
        # 创建一个简单的窗口来容纳查询组件
        self.maintenance_window = QMainWindow()
        self.maintenance_window.setWindowTitle('维修记录查询')
        self.maintenance_window.setGeometry(100, 100, 800, 600)
        # 添加返回按钮
        back_btn = QPushButton('返回')
        back_btn.clicked.connect(self.maintenance_window.close)
        layout = QVBoxLayout()
        layout.addWidget(self.maintenance_query_widget)
        layout.addWidget(back_btn)
        central_widget = QWidget()
        central_widget.setLayout(layout)
        self.maintenance_window.setCentralWidget(central_widget)
        self.maintenance_window.show()
    
    def open_fee_query(self):
        """打开宿舍费用查询窗口"""
        from iteration6_fee_management.gui_fee_management import FeeManagementWindow, QueryFeeWidget
        # 直接创建查询窗口组件
        self.fee_query_widget = QueryFeeWidget()
        # 创建一个简单的窗口来容纳查询组件
        self.fee_window = QMainWindow()
        self.fee_window.setWindowTitle('宿舍费用查询')
        self.fee_window.setGeometry(100, 100, 800, 600)
        # 添加返回按钮
        back_btn = QPushButton('返回')
        back_btn.clicked.connect(self.fee_window.close)
        layout = QVBoxLayout()
        layout.addWidget(self.fee_query_widget)
        layout.addWidget(back_btn)
        central_widget = QWidget()
        central_widget.setLayout(layout)
        self.fee_window.setCentralWidget(central_widget)
        self.fee_window.show()

# 测试代码
if __name__ == "__main__":
    app = QApplication(sys.argv)
    # 创建一个测试用户
    test_user = {'username': 'student', 'role': 'student', 'name': '测试学生'}
    window = StudentWindow(test_user)
    window.show()
    sys.exit(app.exec_())